package com.yao.handler;

import com.yao.VO.ResultVO;
import com.yao.config.ProjectUrlConfig;
import com.yao.exception.ResponseBankException;
import com.yao.exception.SellerAuthorizeException;
import com.yao.exception.WeixinSellException;
import com.yao.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * User: 姚宏武
 * Date: 2018/3/7
 * Time: 17:26
 * Email：401369870@qq.com
 * Description:
 */
@ControllerAdvice
public class SellExceptionHandler {

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    /**
     * 拦截登录异常
     *
     * @return
     */
    @ExceptionHandler(value = SellerAuthorizeException.class)
    public ModelAndView handlerAuthorizeException() {

        // https://open.weixin.qq.com/connect/qrconnect?appid=wx6ad144e54af67d87&redirect_uri=
        // http%3A%2F%2Fsell.springboot.cn%2Fsell%2Fqr%2FoTgZpwVQkAOVQoDOdSdfFNk3lp_E
        // &response_type=code&scope=snsapi_login&state=http://yaohongwu.natappvip.cc/wechat/qrUserInfo
        return new ModelAndView("redirect:"
        .concat("https://open.weixin.qq.com/connect/qrconnect?appid=wx6ad144e54af67d87")
        .concat("&redirect_uri=")
        .concat(projectUrlConfig.getWechatOpenAuthorize())
        .concat("&response_type=code&scope=snsapi_login")
        .concat("&state=")
        .concat(projectUrlConfig.getSell())+"/wechat/qrUserInfo");
    }

    /**
     * 异常统一处理，保持成功或失败，返回前端风格统一
     * @param e
     * @return
     */
    @ExceptionHandler(value = WeixinSellException.class)
    @ResponseBody
    public ResultVO handlerWeixinSellException(WeixinSellException e) {
        return ResultVOUtil.error(e.getCode(),e.getMessage());
    }

    /**
     * 有些地方需要当抛出异常时，需要修改http请求的响应不是200
     */
    @ExceptionHandler(value = ResponseBankException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerResponseBankException(){

    }

}
